﻿(function ($) {

    var jFadeList = {
        options: {
            limit: 6,
            interval: 2000
        },
        $OrgUL: undefined,
        itemList: [],
        total: 0,
        curItem: 0,
        itemsHeight: 0,
        init: function (options) {
            this.options = $.extend({}, this.options, options);

            this.setUp();
            this.makeList();
            this.setTimeout(true);
        },
        setUp: function () {
            var itemList = this.$OrgUL.children('li').clone().css({ opacity: 0, height: 0 });
            console.log(itemList);
            this.itemList = itemList;
            this.total = itemList.length;

            if (this.total < this.options.limit)
                this.options.limit = this.total;

            this.curItem = this.options.limit - 1;

            this.itemsHeight = this.$OrgUL.children('li').first().height();
        },
        makeList: function () {
            this.$OrgUL.children('li:gt(' + (this.curItem) + ')').remove();
        },
        setTimeout: function (next) {
            var jFadeList = this;
            setTimeout(function () {
                if (next)
                    jFadeList.next.call(jFadeList);
                else
                    jFadeList.prev.call(jFadeList);
            }, jFadeList.options.interval);
        },
        next: function () {
            if (this.curItem != this.total - 1)
                this.curItem++;
            else
                this.curItem = 0;

            this.move(true);
        },
        prev: function () {
            if (this.curItem != 0)
                this.curItem--;
            else
                this.curItem = 0;

            this.move(false);
        },
        move: function (Next) {
            if (Next) {
                var jFadeList = this;

                jFadeList.$OrgUL.append(jFadeList.itemList.eq(jFadeList.curItem).clone());

                jFadeList.$OrgUL.children('li:first')
                    .animate({ opacity: 0, height: 0, paddingDown: 0, paddingTop: 0, margin: 0 }, jFadeList.options.interval / 2, function () {
                        $(this).remove();
                    });

                jFadeList.$OrgUL.children('li:last')
                    .animate({ opacity: 1, height: jFadeList.itemsHeight }, jFadeList.options.interval / 2, function () {
                        jFadeList.setTimeout(true);
                    });
            }
            else {
                var jFadeList = this;

                jFadeList.$OrgUL.append(jFadeList.itemList.eq(jFadeList.curItem).clone());

                jFadeList.$OrgUL.children('li:last').hide(jFadeList.options.interval, function () {
                    $(this).remove();
                });

                jFadeList.$OrgUL.children('li:first').show(jFadeList.options.interval, function () {
                    jFadeList.setTimeout(false);
                });
            }
        }
    };

    $.fn.jFadeList = function (options) {
        var $this = $(this);

        jFadeList.$OrgUL = this;
        jFadeList.init(options);
    };
})(jQuery);